package dp;

import java.util.Arrays;

/**
 * leet-code(279) 完全平方数
 *
 * @author Ringo
 * @date 2021/8/14 14:17
 */
public class NumSquares {

    public static void main(String[] args) {
        System.out.println(numSquares(12));
    }

    public static int numSquares(int n) {
        if (n < 0) return 0;
        int[] dp = new int[n + 1];
        dp[0] = 0;
        for (int i = 1; i <= n; ++i) {
            dp[i] = i;
            for (int j = 0; i - j * j >= 0; ++j) {
                // 这里的 +1 指的是 j*j
                dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
            }
        }
        System.out.println(Arrays.toString(dp));
        return dp[n];
    }
}
